# -*- coding: utf-8 -*-
# created on 2016/01/11

from mathsolver.functions.daoshu.daoshuyunsuan import HanShuDaoShuYunSuan
from mathsolver.functions.fangcheng.erci import ErCi005, ErCi006, ErCi008
from mathsolver.functions.hanshu.helper import check_eq, check_func
from mathsolver.functions.base import BaseFunction, BaseNumber


class ErCiHanShuRootGeShuQiuCan(BaseFunction):
    """一元二次方程根的个数求参数"""

    def solver(self, *args):
        eq, n = args[0], args[1].value
        if n == 2:
            return ErCi005().solver(eq)
        elif n == 1:
            return ErCi006().solver(eq)
        elif n == 0:
            return ErCi008().solver(eq)
        else:
            raise ValueError('unknow value n = %f' % n)


class DanDiaoQuJianGeShuQiuCan(BaseFunction):
    """已知函数单调区间个数求参数
    其中的函数的导函数必须是二次函数
    """

    def solver(self, *args):
        func, number = check_func(args[0]), args[1]

        # 求函数导数
        deriv = HanShuDaoShuYunSuan().solver(func).output[0]
        self.steps.append(["因为 {}".format(func.printing()), "求导得 {}".format(deriv.printing())])

        # 转化成导函数的零点个数问题
        root_geshu = number.value - 1
        self.steps.append(["", "f(x) 有 %s 个单调区间，等价为 f'(x) 有 %s 个根" % (number.value, root_geshu)])

        # 一元二次方程根的个数求参数
        step_solver = ErCiHanShuRootGeShuQiuCan().solver(check_eq(deriv), BaseNumber(root_geshu))
        self.steps.extend(step_solver.steps)

        self.output.extend(step_solver.output)
        self.label.add("已知函数单调区间个数求参数")
        return self


if __name__ == '__main__':
    pass
